package com.stu.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * spring-Cache 的不足
 * 1 读模式
 * <span/>缓存穿透：查询一个null数据，解决，缓存空数据 cache-null-values
 * <span/>缓存击穿：大量并发进来同时查询一个正好过期的数据，解决：加锁
 * <span/> 缓存雪崩：大量key同时过期，解决，加随机时间|spring.cache.redis.time-to-live
 * 2 写模式(缓存和数据库一致)
 * <span/>读写加锁
 * <span/>引入 Canal,感知MySQL的更新去更新数据库
 * <span/> 读多写多，直接去数据库查询。
 */
@EnableCaching//开启缓存
@Configuration
// @EnableConfigurationProperties(CacheProperties.class)
public class RedisCacheConfig {

    /**
     * redis序列化 使用JSON
     * https://github.com/alibaba/fastjson/issues/2780
     * TODO https://zhuanlan.zhihu.com/p/138295935
     */
    @Bean
    @ConditionalOnProperty(name = "active.redis", havingValue = "true", matchIfMissing = true)
    public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        config = config.serializeKeysWith(
                RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        config = config.serializeValuesWith(
                RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
        //也可以使用这个 GenericFastJsonRedisSerializer
        //GenericFastJsonRedisSerializer|GenericJackson2JsonRedisSerializer
        //GenericFastJsonRedisSerializer
        //@JsonInclude(JsonInclude.Include.NON_EMPTY) // 设置不为空的时候返回 需要使用 GenericJackson2JsonRedisSerializer 才会生效

        //String ttlStr = environment.getProperty("spring.cache.redis.time-to-live");
        //config = config.entryTtl(Duration.ofSeconds(3600));
        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }
        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixKeysWith(redisProperties.getKeyPrefix());
        }
        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }
        return config;
    }
}
